from zhipuai import ZhipuAI
import re

client = ZhipuAI(api_key="e896525e15c4856390a089cc7e199c46.NU3hO2WLngbwkXmV")

def dinam(dict_str):
    response = client.chat.completions.create(
        model="glm-4",  # 请填写您要调用的模型名称
        messages=[
            {"role": "user", "content": f"你现在是一个服装卖家，f{dict_str}这个是你的产品资料"},
            {"role": "user", "content": "根据资料写卖点一：面料，卖点二：设计卖点，卖点三：搭配，卖点四：场合，卖点五：服装护理"},
            {"role": "user", "content": "把这五个卖点每个都翻译成英文并保存在zyi数组中，以Python数组形式返回，不要注释和多维数组"},
        ],
    )
    response_str = response.choices[0].message.content
    print("（卖点）原参数为：", response.choices[0].message)
    regex = r"zyi = \[(.*?)\]"
    # 使用re.search查找匹配的字符串
    match = re.search(regex, response_str, re.DOTALL)

    # 如果找到匹配项，则提取数组内容
    if match:
        list_str = match.group(1)

        # # 移除字符串中的多余空格和换行符
        list_str = re.sub(r'\s+', ' ', list_str).strip()
        # 将字符串转换为Python数组
        zyi_list = [item.strip("") for item in list_str.split('", "')]
        print("数组为：", zyi_list)
        return zyi_list
    else:
        print("没有找到匹配的数组")
        return None

if __name__ == '__main__':
    dict_str = {'产品名': 'White Plaid Bubble Sleeve Flowy Shirt Dress', '价格': '17.25', '重量': '0.45 kg', '材质': '50%Cotton+45%Polyester+5%Viscose', '细节': 'This plaid dress makes you stand out in style.\nTrendy bubble sleeves add a playful and chic touch to the dress.\nHigh waist detail for a flattering and comfortable fit.\nButtoned front and shirt collar for a polished and versatile look.', '属性值': [{'尺寸': '(US 4-6)S'}, {'尺寸': '(US 8-10)M'}, {'尺寸': '(US 12-14)L'}, {'尺寸': '(US 16-18)XL'}], '规格': [['Item No', 'LC6121673-P122'], ['Product Weight', '0.45Kg'], ['Category', 'Dresses > Mini Dresses'], ['Creation Time', '2024-08-01'], ['Material', '50%Cotton+45%Polyester+5%Viscose'], ['Sleeve Length', 'Long Sleeve'], ['Waist Line', 'High Waist'], ['Style', 'casual'], ['Silhouette', 'Shift'], ['Occasion', 'Daily'], ['Dresses Length', 'Mini'], ['Color', 'white'], ['Pattern', 'plaid'], ['Neckline', 'turn-down collar']]}

    dinam(dict_str)
